import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_problem.dart';
import 'package:flutter_learn_vi/src/page/topic/topic_enum.dart';
import 'package:flutter_learn_vi/src/page/topic/topic_item_gapfill_widget.dart';
import 'package:provider/provider.dart';

import 'data/topic_text_model.dart';
import 'topic_item_single_widget.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/12/13.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class TopicDetailsItemPage extends StatefulWidget {
  final ProblemModel problemModel;
  final bool isCheckError;

  //是否实时显示答案
  final bool isShowError;

  ///答题类型
  final TopicEnum topicEnum;

  //是否可以多次修改
  final bool isUpdateError;

  TopicDetailsItemPage({
    this.problemModel,
    this.isCheckError,
    @required this.topicEnum,
    this.isShowError = true,
    this.isUpdateError = false,
    Key key,
  }) : super(key: key);

  @override
  _TopicDetailsItemPageState createState() => _TopicDetailsItemPageState();
}

class _TopicDetailsItemPageState extends State<TopicDetailsItemPage> {
  @override
  void initState() {
    super.initState();
    if (widget.isCheckError) {
      widget.problemModel.themAnswerStr = widget.problemModel.answer;
    }
  }

  TextSizeBean _textSizeBean;

  @override
  Widget build(BuildContext context) {
    return Consumer<TopicTextModel>(builder:
        (BuildContext context, TopicTextModel textModel, Widget child) {
      _textSizeBean = textModel.textSizeBean;
      return buildContainer();
    });
  }

  Container buildContainer() {
    return Container(
      width: double.infinity,
      height: double.infinity,
      padding: EdgeInsets.all(10),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(
            height: 6,
          ),
          Container(
            margin: EdgeInsets.only(bottom: 6),
            child: Text(
              widget.problemModel.title
                  .replaceAll("<p>", "")
                  .replaceAll("<\/p>", ""),
              style: TextStyle(
                  fontSize: _textSizeBean.titleSize,
                  fontWeight: FontWeight.w500),
            ),
          ),
          Container(
            height: 33,
            child: Stack(
              alignment: Alignment.center,
              children: [
                Divider(),
                buildTopicType(),
              ],
            ),
          ),
          SizedBox(
            height: 10,
          ),
          Expanded(
            child: SingleChildScrollView(
              child: buildSelectItem(),
            ),
          ),
          buildShowAnswerWidget(),
        ],
      ),
    );
  }

  buildShowAnswerWidget() {
    if (!widget.isCheckError) {
      return Container();
    }
    return Container(
      margin: EdgeInsets.only(bottom: 20, left: 18),
      child: Row(
        children: [
          Text("正确答案为 ${widget.problemModel.answer}"),
        ],
      ),
    );
  }

  Widget buildTopicType() {
    String type = "单选题";
    Decoration decoration;
    switch (widget.problemModel.questionType) {
      case QuestionType.SINGLE_CHOICE:
        type = "单选题";
        decoration = BoxDecoration(
            border: Border.all(color: Colors.redAccent),
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(10),
              bottomRight: Radius.circular(10),
            ));
        break;
      case QuestionType.MULTIPLE_CHOICE:
        type = "多选题";
        decoration = BoxDecoration(
          border: Border.all(color: Colors.blue),
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(10),
            topRight: Radius.circular(10),
          ),
        );
        break;
      case QuestionType.IUDGE:
        type = "判断题";
        decoration = BoxDecoration(
          border: Border.all(color: Colors.deepOrangeAccent),
          borderRadius: BorderRadius.all(
            Radius.circular(10),
          ),
        );
        break;
      case QuestionType.GAPFILL_CHOICE:
        type = "填空题";
        decoration = BoxDecoration(
          border: Border.all(color: Colors.deepPurpleAccent),
          borderRadius: BorderRadius.only(
            bottomLeft: Radius.circular(10),
            topRight: Radius.circular(10),
          ),
        );
        break;
      case QuestionType.NONE:
        type = "未知";
        break;
    }
    return Container(
      padding: EdgeInsets.only(left: 6, right: 6, top: 2, bottom: 2),
      decoration: decoration,
      child: Text(
        "$type",
        style: TextStyle(fontSize: _textSizeBean.typeSize),
      ),
    );
  }

  String singleGroup = "";

  buildSelectItem() {
    Widget itemWidget = Container();
    switch (widget.problemModel.questionType) {
      case QuestionType.SINGLE_CHOICE:
        itemWidget = TopicSingleWidget(
          widget.problemModel,
          isMyError: widget.isShowError,
          isUpdateError: widget.isUpdateError,
          topicEnum: widget.topicEnum,
        );
        break;
      case QuestionType.MULTIPLE_CHOICE:
        itemWidget = TopicSingleWidget(
          widget.problemModel,
          isMul: true,
          isMyError: widget.isShowError,
          isUpdateError: widget.isUpdateError,
          topicEnum: widget.topicEnum,
        );
        break;
      case QuestionType.IUDGE:
        itemWidget = TopicSingleWidget(
          widget.problemModel,
          isMyError: widget.isShowError,
          isUpdateError: widget.isUpdateError,
          topicEnum: widget.topicEnum,
        );
        break;
      case QuestionType.GAPFILL_CHOICE:
        itemWidget =
            TopicGapfillWidget(widget.problemModel, widget.isCheckError);
        break;
      case QuestionType.NONE:
        itemWidget = Container(
          child: Text("未知题型"),
        );

        break;
    }

    return itemWidget;
  }
}
